﻿Imports System.Data.OleDb

Public Class AddPlayer
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim clubID As String = Request.Params("ClubID")

            If Not String.IsNullOrEmpty(clubID) Then

                Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
                oleDbConn.Open()

                Dim selectClubSQL As String = "SELECT ClubName FROM Clubs WHERE ID=@f1"
                Dim selectClubCmd As OleDbCommand = New OleDbCommand(selectClubSQL, oleDbConn)
                selectClubCmd.CommandType = CommandType.Text
                selectClubCmd.Parameters.AddWithValue("@f1", Request.Params("ClubID"))
                Dim selectClubDr As String = selectClubCmd.ExecuteScalar()

                If Not String.IsNullOrEmpty(selectClubDr) Then
                    ddlClubName.Enabled = False
                    ddlClubName.SelectedValue = clubID
                Else
                    Session("UserMessage_Error") = "The specified club does not exist!"
                    Response.Redirect("~/ClubList.aspx")
                End If

                selectClubCmd.Dispose()
                oleDbConn.Close()
                oleDbConn.Dispose()
            Else
                ddlClubName.Enabled = True
            End If
        End If

    End Sub

    Protected Sub btnAddPlayer_Click(sender As Object, e As EventArgs) Handles btnAddPlayer.Click
        If Page.IsValid Then

            Dim newFileName As String

            If fuProfilePicture.HasFile Then
                Dim myGUID As New System.Guid
                myGUID = Guid.NewGuid
                newFileName = myGUID.ToString + ".jpg"
                Dim picLocationOnServerHardDisk = Request.MapPath("Players") & "/" & newFileName
                fuProfilePicture.SaveAs(picLocationOnServerHardDisk)
            Else
                newFileName = "profile.jpg"
            End If

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            Dim createPlayerSQL As String = "INSERT INTO Players(FullName,ClubID_FK,DateOfBirth,NationalityID_FK,Height,Weight," +
                                      "PositionID_FK,PreferredFoot,ProfilePicture,Username,CreatedAt,UpdatedAt) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8,@f9,@f10,@f11,@f12)"
            Dim createPlayerCmd As OleDbCommand = New OleDbCommand(createPlayerSQL, oleDbConn)
            createPlayerCmd.CommandType = CommandType.Text
            createPlayerCmd.Parameters.AddWithValue("@f1", txtFullName.Text)
            createPlayerCmd.Parameters.AddWithValue("@f2", ddlClubName.SelectedValue)
            createPlayerCmd.Parameters.AddWithValue("@f3", txtDateOfBirth.Text)
            createPlayerCmd.Parameters.AddWithValue("@f4", ddlNationality.SelectedValue)
            createPlayerCmd.Parameters.AddWithValue("@f5", txtHeight.Text)
            createPlayerCmd.Parameters.AddWithValue("@f6", txtWeight.Text)
            createPlayerCmd.Parameters.AddWithValue("@f7", ddlPosition.SelectedValue)

            If rblPreferredFoot.SelectedValue = "Left" Then
                createPlayerCmd.Parameters.AddWithValue("@f8", "Left")
            Else
                createPlayerCmd.Parameters.AddWithValue("@f8", "Right")
            End If

            createPlayerCmd.Parameters.AddWithValue("@f9", "Players/" & newFileName)
            createPlayerCmd.Parameters.AddWithValue("@f10", User.Identity.Name)
            createPlayerCmd.Parameters.AddWithValue("@f11", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
            createPlayerCmd.Parameters.AddWithValue("@f12", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))

            oleDbConn.Open()
            createPlayerCmd.ExecuteNonQuery()

            System.Threading.Thread.Sleep(500)
            Session("UserMessage") = "The player was successfully added!"

            Response.Redirect("~/PlayerList.aspx?ClubID=" + ddlClubName.SelectedValue)

            createPlayerCmd.Dispose()
            oleDbConn.Close()
            oleDbConn.Dispose()
        Else
            Session("UserMessage_Error") = "There was a problem with the data you submitted. Please try again."
            Response.Redirect(Request.RawUrl)
        End If
    End Sub

    Protected Sub btnBackClubList_Click(sender As Object, e As EventArgs) Handles btnBackClubList.Click
        Response.Redirect("~/ClubList.aspx")
    End Sub
End Class